/*
 * @Author: chichiksky
 * @Date: 2022-02-14 11:33:34
 * @LastEditTime: 2022-03-17 17:29:43
 * @LastEditors: your name
 * @Description: 
 * @FilePath: \web\src\assets\common\cookie.js
 */
const Cookie = {
  /**
   * 设置一个cookie
   * @method set
   * @param {String} cname cookie名
   * @param {String} cvalue cookie值
   * @param {Number} exdays 过期时间
   */
  set : function (cname, cvalue, path, exdays) {
    var str =  cname + "=" + cvalue;
    if(exdays) {
      var d = new Date();
      d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
      str += "; expires=" + d.toGMTString();
    }
    if(path) {
      str += "; path=" + path
    } else {
      str += "; path=/"
    }
    document.cookie = str;
  },
  /**
   * 获取指定cookie的值
   * @method get
   * @param {String} cname cookie名
   * @return  cookie值
   */
  get : function (cname) {
    var v = document.cookie.match('(?:^|;)\\s*' + cname + '=([^;]*)');
    return v ? v[1] : null;
  },
  /**
   * 删除指定cookie,复写为过期
   * @method remove
   * @param {String} cname cookie名
   * @param {String} path 路径
   */
  remove : function (cname, path) {
    this.set(cname,'',path,-1);
  }
}
export default Cookie;